/*
 * 文件名：HomeController.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2016年5月30日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.zcsy.seller.web.system;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.manage.model.base.CommonImg;
import com.zcsy.manage.model.base.ShopInfo;
import com.zcsy.manage.model.mall.CategoryAttr;
import com.zcsy.manage.model.mall.GoodsCategory;
import com.zcsy.manage.model.mall.GoodsInfo;
import com.zcsy.manage.model.mall.OrderDetailInfoView;
import com.zcsy.manage.model.mall.OrderInfo;
import com.zcsy.manage.model.news.NoticeInfo;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.persistence.mall.OrderDetailInfoDao;
import com.zcsy.manage.persistence.weixin.StringUtil;
import com.zcsy.manage.service.base.ICommonImgService;
import com.zcsy.manage.service.base.IShopInfoService;
import com.zcsy.manage.service.mall.ICategoryAttrService;
import com.zcsy.manage.service.mall.IGoodsCategoryService;
import com.zcsy.manage.service.mall.IGoodsInfoService;
import com.zcsy.manage.service.mall.IOrderInfoService;
import com.zcsy.manage.service.news.INoticeInfoService;
import com.zcsy.manage.service.system.IDictService;

/**
 * @Title: HomeController.java
 * @Package： com.zcsy.seller.web.system 
 * @Description: 首页控制层
 * @author liuying 1075817474@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年5月30日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/home")
public class HomeController {
	
	@Autowired
	private IShopInfoService shopInfoService;
	
	@Autowired
	private INoticeInfoService noticeInfoService;
	
	@Autowired
	private IGoodsInfoService goodsInfoService;
	
	@Autowired
	private IGoodsCategoryService goodsCategoryService;
	
	@Autowired
	private IDictService dictService;
	
	@Autowired
	private ICategoryAttrService categoryAttrService;
	
	@Autowired
	private ICommonImgService commonImgService;
	
	@Autowired
	private IOrderInfoService orderInfoService;
	
	@Autowired
	private OrderDetailInfoDao orderDetailInfoDao;
	
    /**
     * @Description 进入首页
     * @param request 用户请求
     * @return 跳转页面
     * @author liuying 1075817474@qq.com
     * @date 2016年5月30日
     * @see 
     */
    @RequestMapping(value = "homePage", method = RequestMethod.GET)
    public String homePage(Model model, HttpServletRequest req){
    	
    	ShopInfo shopInfo = (ShopInfo)req.getSession().getAttribute(RequestConstants.SHOP_ID);
    	
    	//将电话号码中间四位隐藏起来
    	String phone = shopInfo.getPhone();
    	if(!StringUtil.isEmptyString(phone)) {
    	    phone = phone.substring(0,phone.length()-(phone.substring(3)).length())+"****"+phone.substring(7);
    	}
    	shopInfo.setPhone(phone);
    	
    	//获取小区通知列表(不多于五个)
    	String cellId = shopInfo.getCellId();
        User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        List<NoticeInfo> noticeInfos = noticeInfoService.selectCountList(user.getId(),cellId);
        if(noticeInfos.size() >=4){
        	noticeInfos = noticeInfos.subList(0, 4);
        }
        
        //获取店内热销产品
        String shopId = shopInfo.getId();
        List<GoodsInfo> goodsInfos = goodsInfoService.getGoodsInfoBySales(shopId);
        if(goodsInfos.size() >=5){
        	goodsInfos = goodsInfos.subList(0, 5);
        }
        
        //获取当前店铺待付款订单
        List<OrderInfo> waitPayMoneyOrderList = orderInfoService.selectOrderListByShopIdStates(shopInfo.getId(), Constants.SELLER_WAIT_PAY);
        model.addAttribute("waitPayMoneyOrderList", waitPayMoneyOrderList);
        
        //获取当前店铺待确认订单
        List<OrderInfo> waitComfirmOrderList = orderInfoService.selectOrderListByShopIdStates(shopInfo.getId(), Constants.SELLER_WAIT_COMFIRM);
        model.addAttribute("waitComfirmOrderList", waitComfirmOrderList);
        
        //获取当前店铺待发货订单
        List<OrderInfo> waitSendOrderList = orderInfoService.selectOrderListByShopIdStates(shopInfo.getId(), Constants.SELLER_WAIT_SEND);
        model.addAttribute("waitSendOrderList", waitSendOrderList);
        
        //获取当前店铺退款订单
        List<OrderInfo> waitRefundOrderList = orderInfoService.selectOrderListByShopIdStates(shopInfo.getId(), Constants.SELLER_REFUND);
        model.addAttribute("waitRefundOrderList", waitRefundOrderList);
        
        List<OrderInfo> untreatOrderInfos = orderInfoService.selectOrderListByShopIdStates(shopInfo.getId(), Constants.SELLER_WAIT_TREAT);
        List<OrderDetailInfoView> orderDetails = new ArrayList<OrderDetailInfoView>();
        List<OrderInfo> orderInfos = new ArrayList<OrderInfo>();
        if(null != untreatOrderInfos){
        	if(untreatOrderInfos.size() <= 3){
        		for (OrderInfo orderInfo : untreatOrderInfos) {
        			orderDetails = orderDetailInfoDao.getByOrderId(orderInfo.getId());
        			orderInfo.setOrderDetails(orderDetails);
        			orderInfos.add(orderInfo);
        		}
        	} else {
        		for(int i = 0; i < 3; i++){
        			OrderInfo orderInfo = untreatOrderInfos.get(i);
        			orderDetails = orderDetailInfoDao.getByOrderId(orderInfo.getId());
        			orderInfo.setOrderDetails(orderDetails);
        			orderInfos.add(orderInfo);
        		}
        	}
        }
		
    	model.addAttribute("shopInfo", shopInfo);
    	model.addAttribute("noticeInfos", noticeInfos);
    	model.addAttribute("goodsInfos", goodsInfos);
    	model.addAttribute("orderInfos", orderInfos);
        
        return "homePage";
    }
	
    /**
     * @Description 更新店铺状态
     * @param id 店铺id
     * @param request 用户请求
     * @return 更新结果
     * @author liuying 1075817474@qq.com
     * @date 2016年6月1日
     * @see 
     */
    @RequestMapping(value = "UpdateShopState", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson UpdateShopState(String id, int state, HttpServletRequest request){
		RequestJson result = new RequestJson();
        try {
        	ShopInfo record = shopInfoService.selectByPrimaryKey(id);
        	
        	if(state == Constants.SHOP_STATE_NORMAL){
                record.setState(Constants.SHOP_STATE_SUSPEND);
        	}
        	if(state == Constants.SHOP_STATE_SUSPEND){
        		record.setState(Constants.SHOP_STATE_NORMAL);
        	}
            shopInfoService.updateShopState(record);
            
            ShopInfo shopInfo = (ShopInfo)request.getSession().getAttribute(RequestConstants.SHOP_ID);
            shopInfo.setState(record.getState());
            
        }catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }
    
    /**
     * @Description 增加商品
     * @param id 商品id
     * @return
     */
    @RequestMapping(value = "editGoodsInfo", method = RequestMethod.GET)
    public String editGoodsInfo(Model model, String id) {
        try {
            if (String.valueOf(Constants.STATE_DISABLED).equals(id)) {
                model.addAttribute("type", Constants.SAVE_TYPE);
            } else {
                GoodsInfo goodsInfo = goodsInfoService.getGoodsInfoByPrimaryKey(id);
                if(goodsInfo != null){
                	model.addAttribute("type", Constants.EDIT_TYPE);
                	model.addAttribute("goodsInfo", goodsInfo);
                	GoodsCategory goodsCategory = goodsCategoryService.getGoodsCategoryById(goodsInfo.getGoodsCategoryId());
                	model.addAttribute("goodsCategory", goodsCategory);
                	model.addAttribute("secondCategory", goodsCategoryService.getGoodsCategoryByParentId(goodsCategory.getParentId()));
                	
                	String[] key = goodsInfo.getKeyword().split("\\,");
                	String keyword = StringUtils.join(key, "|");
                	model.addAttribute("keyword", keyword);
                	
                	//根据分类id获取所有的商品分类属性
                	List<CategoryAttr> categoryAttrs = categoryAttrService.selectByCategoryId(goodsInfo.getGoodsCategoryId());
                	
                	model.addAttribute("categoryAttrs", categoryAttrs);
                	
                	//获取商品banner图
                	List<CommonImg> imgs = commonImgService.selectImgsByObjIdAndObjType(id, Constants.IMG_GOODS_BANNER);
                	model.addAttribute("imgs", imgs);
                }
            }
            //获取所有一级商品分类
            model.addAttribute("firstCategory", goodsCategoryService.getGoodsCategoryByLevel(1));
        
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "homeGoodsInfoEdit";
    }
}
